// mircoApp/customer_file/fille_in/fill_in.js
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
var app = getApp();
Page({

  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    tel: "",
    name: "",
    form: [
      {
          operation: { txt: "删除" },
          data: [
              { field: 'name', label: "项目", placeholder: "请输入项目名称", value: "" },
              { field: 'teacher_name', label: "操作老师", placeholder: "请输入姓名", value: "" },
              { field: 'price', label: "价格", placeholder: "请输入价格", value: "" },
              { field: "remark", label: "备注", placeholder: "请输入备注", value: "" },
              { field: 'upload', label: "图片", fileList: [],image_list :[]}
          ]
      }
    ] ,
    timerId:null

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  handle() {
    let copy_data = [
      {
          operation: { txt: "删除" },
          data: [
              { field: 'name', label: "项目", placeholder: "请输入项目名称", value: "" },
              { field: 'teacher_name', label: "操作老师", placeholder: "请输入姓名", value: "" },
              { field: 'price', label: "价格", placeholder: "请输入价格", value: "" },
              { field: "remark", label: "备注", placeholder: "请输入备注", value: "" },
              { field: 'upload', label: "图片", fileList: [] ,image_list:[]}
          ]
      }
  ];
    this.data.form = this.data.form.concat(copy_data);
    this.setData({form:this.data.form})
  },
  del(e){
    let index = e.currentTarget.dataset.index;
    this.data.form.splice(index,1);
    this.setData({form:this.data.form});
  },

  afterRead(event) {
    let {
      index
    } = event.currentTarget.dataset;
    let {
      file
    } = event.detail;
    console.log(event,index)
    wx.showLoading({
      title: '上传中',
    })

    if (file && file.length) {
      for (let i = 0; i < file.length; i++) {
        this.onRead_1(file[i], index)
      }
    } else {
      this.onRead_1(file, index)
    }

  },
  onRead_1(file, index) {
    let urlStr = app.getNetAddresss('upload.uploadPic');
    wx.uploadFile({
      url: urlStr,
      filePath: file.url,
      name: 'file',
      formData: null,
      success: (res) => {
        wx.hideLoading();
        let _res = JSON.parse(res.data);
        this.data.form[index].data[this.data.form[index].data.length-1].image_list.push(_res.data.img_url);
        this.data.form[index].data[this.data.form[index].data.length-1].fileList.push({ ...file, url: _res.data.img_url });
        this.setData({form:this.data.form});
      },
      fail: (err) => {
        wx.hideLoading()
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },
  inpMini(e){
    let {index,cur_index} = e.currentTarget.dataset;
    this.data.form[index].data[cur_index].value = e.detail;
    this.setData({form:this.data.form});
  },
  submit(){
    let project_list = []
    this.data.form.forEach((item, index) => {
        project_list[index] = {};
        item.data.forEach(_item => {
            if (_item.field == 'upload') {
                project_list[index]['image_list'] = _item.image_list;
            } else {
                project_list[index][_item.field] = _item.value;

            }

        })
    })
    let json = {
        form: {
            name: this.data.name,
            mobile: Number(this.data.tel),
            project_list: project_list
        }
    }
    app._postNetWork({
      url: app.getNetAddresss("plugin.customer-archive.frontend.customer-archive.add-customer"),
      data: json,
      success: resdata => {
        let {
          result,
          msg,
        } = resdata.data;
        app.tips(msg);
        if (result) {
         this.data.timerId =  setTimeout(() => {
            wx.navigateBack();
          },500);
        }
      }
    })

  },
  handle_delete(event){
    let index = event.currentTarget.dataset.index;
    let file_index = event.detail.index;
    this.data.form[index].data[this.data.form[index].data.length-1].image_list.splice(file_index,1);
    this.data.form[index].data[this.data.form[index].data.length-1].fileList.splice(file_index,1);
    this.setData({form:this.data.form});
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    this.data.timerId = null;
    clearTimeout(this.data.timerId);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})